package main

import (
	"log"

	"io/ioutil"

	"os"

	print "github.com/alexbrainman/printer"
	"github.com/jadefox10200/goprint"
)

func main() {

	printerName, _ := goprint.GetDefaultPrinterName()

	//open the printer
	printerHandle, err := goprint.GoOpenPrinter(printerName)
	if err != nil {
		log.Fatalln("Failed to open printer")
	}
	defer goprint.GoClosePrinter(printerHandle)

	filePath := "file/1.html"
	//filePath := "file/1.pdf"
	//Send to printer:
	err = goprint.GoPrint(printerHandle, filePath)
	if err != nil {
		log.Fatalln("during the func sendToPrinter, there was an error")
	}
	Print()
}

func Print() {
	name, err := print.Default() // returns name of Default Printer as string
	if err != nil {
		log.Println(err)
	}
	log.Println(name)
	p, err := print.Open(name) // Opens the named printer and returns a *Printer
	if err != nil {
		log.Println(err)
	}

	err = p.StartDocument("goods_detail", "TEXT") // test: doc name, text: doc type
	if err != nil {
		log.Println(err)
	}
	os.Exit(3)
	err = p.StartPage() // begin a new page
	if err != nil {
		log.Println(err)
	}
	log.Println("length")

	//content := "测试测试 \r\n \r\n "
	//enc := mahonia.NewEncoder("gbk")
	//content = enc.ConvertString(content)
	content, err := ioutil.ReadFile("file/1.html")

	//var contentLen uintptr = uintptr(len(content))
	//var writtenLen int
	log.Println("-------44444----------")
	log.Println(content)

	_, err = p.Write([]byte(content)) // Send some text to the printer
	if err != nil {
		log.Println(err)
	}

	err = p.EndPage() // begin a new page
	if err != nil {
		log.Println(err)
	}

	err = p.EndDocument() // test: doc name, text: doc type
	if err != nil {
		log.Println(err)
	}

	err = p.Close() // close the resource
	if err != nil {
		log.Println(err)
	}
}
